/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.table;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JToolTip;
import javax.swing.ListSelectionModel;
import javax.swing.ToolTipManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class ToolTipJTable extends JTable {
	private static final long serialVersionUID = 454269703200362434L;
	
	private boolean isEnabledToolTips = false;
	
	/**
	 * @see JTable
	 */
	public ToolTipJTable() {
		super();
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(TableModel dm) {
		super(dm);
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(TableModel dm, TableColumnModel cm) {
		super(dm, cm);
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(int numRows, int numColumns) {
		super(numRows, numColumns);
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(Vector<?> rowData, Vector<?> columnNames) {
		super(rowData, columnNames);
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(Object[][] rowData, Object[] columnNames) {
		super(rowData, columnNames);
		setEnabledToolTips(true);
	}
	/**
	 * @see JTable
	 */
	public ToolTipJTable(TableModel dm, TableColumnModel cm,
			ListSelectionModel sm) {
		super(dm, cm, sm);
		setEnabledToolTips(true);
	}
	
	@Override
	public String getToolTipText(MouseEvent event) {
		String str = null;
		
		int column = columnAtPoint(event.getPoint());
		int row = rowAtPoint(event.getPoint());
		
		TableCellRenderer cellRenderer = getCellRenderer(row, column);
		JLabel label = 
				(JLabel)prepareRenderer(cellRenderer, row, column);
		
		str = label.getText();
		
		str = str == null || "".equals(str) ? null : str;
		
		if(str != null) {
			str = "<html>" + str + "</html>";
			str = str.replaceAll("(\n|\r|\n\r|\r\n)", "<br>");
			
		}
		
		return str;
	}
	
	@Override
	public Point getToolTipLocation(MouseEvent event) {
		String tipText = getToolTipText(event);
		if(tipText == null || tipText.equals("")) {
			return null;
		}
		
		int column = this.columnAtPoint(event.getPoint());
		int row = this.rowAtPoint(event.getPoint());
		
		return getCellRect(row, column, false).getLocation();
	}
	@Override
	public JToolTip createToolTip() {
		JToolTip tip = super.createToolTip();
		
		tip.setFont(getFont());
		
		return tip;
	}
	
	/**
	 * Enables the showing of tool tips
	 * @param b true, enables tool tips, otherwise disables
	 */
	public void setEnabledToolTips(boolean b) {
		if(b && !isEnabledToolTips())
			ToolTipManager.sharedInstance().registerComponent(this);
		else if(!b && isEnabledToolTips())
			ToolTipManager.sharedInstance().unregisterComponent(this);
	}
	
	/**
	 * Returns the state of showing of tool tips
	 * @return if <code>true</code> tool tips are showing, 
	 * otherwise <code>false</code>
	 */
	public boolean isEnabledToolTips() {
		return this.isEnabledToolTips;
	}
}
